# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=libsndfile
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.0.29
pkgrel=2
pkgdesc="A C library for reading and writing files containing sampled sound (mingw-w64)"
arch=('any')
url="http://www.mega-nerd.com/libsndfile"
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-flac"
         "${MINGW_PACKAGE_PREFIX}-libogg"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-opus")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('staticlibs' 'strip')
source=("https://github.com/erikd/libsndfile/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.bz2"
        "001-cmake-build-fixes.patch")
sha256sums=('2ba20d44817c8176f097ab25eff44ef0aeec9e00973def5a7174c5ae0764b22f'
            'cecda39c0a00ba9c0236fc97fb30e43048106189b1d824d8302a429dff67771e')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  patch -Np1 -i "${srcdir}"/001-cmake-build-fixes.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  msg2 "Build static for ${MINGW_CHOST}"
  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-static" && cd "${srcdir}/build-${MINGW_CHOST}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DBUILD_SHARED_LIBS=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./

  msg2 "Build shared for ${MINGW_CHOST}"
  [[ -d "${srcdir}/build-${MINGW_CHOST}-shared" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-shared"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-shared" && cd "${srcdir}/build-${MINGW_CHOST}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DBUILD_SHARED_LIBS=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./
}

package() {
  msg2 "Install static for ${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install

  msg2 "Install shared for ${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install
}
